package com.squeekeek.util.japandict;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class JSearch {
	private RomaKanaConv rkconv = new RomaKanaConv();
	private BufferedReader bfh = null;
	
	public JSearch() {
		try {
			this.bfh = new BufferedReader(new InputStreamReader(new FileInputStream("edict2"), "EUC_JP"));
		}
		catch(UnsupportedEncodingException ueee) {
			
		}
		catch(FileNotFoundException fnfe) {
			
		}
	}
	
	public List <HashMap <String, Object>> searchRomaji(String romajiterm) {
		String kanaterms;
		String[] kanagloss;
		String nextline;
		Pattern mypatt;
		Matcher mymatches;
		List <HashMap <String, Object>> results = new ArrayList <HashMap <String, Object>> ();
		
		kanaterms = this.rkconv.getKana(romajiterm, RomaKanaConv.TYPE_HIRA);
		
		try {
			mypatt = Pattern.compile(kanaterms);
			
			while((nextline = this.bfh.readLine()) != null) {
				kanagloss = nextline.substring(nextline.indexOf('[') + 1, nextline.indexOf(']')).split(";");
				
				for(String eachgloss : kanagloss) {
					mymatches = mypatt.matcher(eachgloss);
					
					if(mymatches.lookingAt()) {
						results.add(searchFormatter(nextline));
					}
				}
			}
		}
		catch(IOException ioe) {
			
		}
			
		return results;
	}
	
	/**
	 * Parse a line in the dictionary into a HashMap.
	 * 
	 * Format:
	 * "kanji" => new String [] of kanji
	 * "kana" => new String [] of kana
	 * "geninfo" => new String[] of kana
	 * "gloss" => new String[] of kana
	 * 
	 * @param dictline
	 * @return
	 */
	private HashMap <String, Object> searchFormatter(String dictline) {
		HashMap <String, Object> formatted = new HashMap <String, Object> ();
		
		
		
		return formatted;
	}
}
